package com.example.myapplication3.module;

import android.util.Log;

import com.example.myapplication3.bean.Beijing;
import com.example.myapplication3.bean.Student1;
import com.example.myapplication3.bean.Student2;
import com.example.myapplication3.bean.Student3;
import com.example.myapplication3.bean.Student4;
import com.example.myapplication3.api.ServiceApi;
import com.example.myapplication3.interfaces.Callback;
import com.example.myapplication3.interfaces.IHome;
import com.example.myapplication3.nev.HttpManager;
import com.example.myapplication3.nev.HttpManagerLiss;

import java.io.IOException;
import java.util.Map;

import retrofit2.Call;

import retrofit2.Response;

public class HomeModel {
    public void getCity(final Callback callback) {
        ServiceApi service = HttpManager.getInstance().getService();
        service.gethome1().enqueue(new retrofit2.Callback<Student1>() {
            @Override
            public void onResponse(Call<Student1> call, Response<Student1> response) {
                Student1 body = response.body();
                Log.e("TAG", "11111onResponse: "+body.getData().get(0).getCity());
                callback.success(body);
            }

            @Override
            public void onFailure(Call<Student1> call, Throwable t) {
                callback.fail(t.getMessage());
            }
        });
    }

    public void getCity2(Map<String,String>map,Callback callback) {
        ServiceApi service = HttpManager.getInstance().getService();
        service.gethome2(map).enqueue(new retrofit2.Callback<Student2>() {
            @Override
            public void onResponse(Call<Student2> call, Response<Student2> response) {
                if(response.code()==200){
                    callback.success(response.body());
                }else{
                    try {
                        String str = response.errorBody().string().toString();
                        callback.fail(str);
                    } catch (IOException e) {
                        callback.fail(e.getMessage());
                    }
                }
            }

            @Override
            public void onFailure(Call<Student2> call, Throwable t) {
                callback.fail(t.getMessage());
            }
        });
    }

    public void getCity3(Map<String,String>map,Callback callback) {
        ServiceApi service = HttpManager.getInstance().getService();
        service.gethome3(map).enqueue(new retrofit2.Callback<Student3>() {
            @Override
            public void onResponse(Call<Student3> call, Response<Student3> response) {
              if(response.code()==200){
                  callback.success(response.body());
              }else{
                  try {
                      String str = response.errorBody().string().toString();
                      callback.fail(str);
                  } catch (IOException e) {
                      callback.fail(e.getMessage());
                  }
              }
            }

            @Override
            public void onFailure(Call<Student3> call, Throwable t) {
                callback.fail(t.getMessage());
            }
        });
    }

    public void getCity4(Map<String,String>map,Callback callback) {
        ServiceApi service = HttpManager.getInstance().getService();
        service.gethome4(map).enqueue(new retrofit2.Callback<Student4>() {
            @Override
            public void onResponse(Call<Student4> call, Response<Student4> response) {
                if(response.code()==200){
                    callback.success(response.body());
                }else{
                    try {
                        String str = response.errorBody().string().toString();
                        callback.fail(str);
                    } catch (IOException e) {
                        callback.fail(e.getMessage());
                    }

                }
            }

            @Override
            public void onFailure(Call<Student4> call, Throwable t) {
                callback.fail(t.getMessage());
            }
        });
    }

        public void getCity5(Callback callback) {
        ServiceApi service = HttpManagerLiss.getInstance().getService();
        service.gethome5().enqueue(new retrofit2.Callback<Beijing>() {
            @Override
            public void onResponse(Call<Beijing> call, Response<Beijing> response) {
                callback.success(response.body());
            }

            @Override
            public void onFailure(Call<Beijing> call, Throwable t) {
                callback.fail(t.getMessage());
            }
        });
    }
}





